package com.example.memosystem.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import androidx.annotation.Nullable;


public class DBUtil extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "db_memo.db";//数据库名称
    public static SQLiteDatabase db = null;//用他来操作数据库
    private static final int DATABASE_VERSION = 3;//每次对数据库操作都要加一



    public DBUtil(@Nullable Context context) {
        super(context, DATABASE_NAME, null,DATABASE_VERSION, null);
    }


    @Override
    public void onCreate(SQLiteDatabase db) { //数据库初始化加载代码
        db.execSQL("PRAGMA foregin_keys=false");
        db.execSQL("drop table if exists d_admin");
        //账号 密码 姓名 性别 电话 年龄
        db.execSQL("CREATE TABLE d_admin(" +
                "s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_sex varchar(20)," +
                "s_phone varchar(20)," +
                "s_age varchar(20))");
        db.execSQL("INSERT INTO d_admin VALUES('root', '123456', '张三', '男', '1288333', '18')");
        //管理员表

        db.execSQL("PRAGMA foregin_keys=false");
        db.execSQL("drop table if exists d_record");
        //编号 标题 内容 时间
        db.execSQL("CREATE TABLE d_record(" +
                "s_id varchar(20) primary key," +
                "s_uid varchar(20)," +
                "s_title varchar(20)," +
                "s_con text(20)," +
                "s_time varchar(20))");
        db.execSQL("INSERT INTO d_record VALUES('1', 'root', '上课', '数据结构', '2022年9月10日 上午 12：56')");


        //db.execSQL("PRAGMA foregin_key=true");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//数据库变动执行
        onCreate(db);
    }
}
